<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateUserGroup{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $category = null){
        
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateGroupName = new Zend_Validate();
        $validateDescription = new Zend_Validate();
        
        $option = array('table' => 'user_groups', 'field' => 'id');
        
        $validateGroupName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_Alnum(true), true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 50)), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($option), true);
                        
        $validateDescription->addValidator(new Zend_Validate_NotEmpty(), true)
                            ->addValidator(new Zend_Validate_Alnum(true), true)
                            ->addValidator(new Zend_Validate_StringLength(array('max' => 100)), true);
        
        /*
        * Check Category
        */
        
        if(!$validateGroupName->isValid($params['group_name'])){
            $this->_messageError['group_name'] = '<label class="control-label" for="inputError">' . $translate->_('group name') . current($validateGroupName->getMessages()) . '</label>';
            $this->_arrData['group_name'] = '';
            $this->_classError['group_name'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['group_name'] = $category['group_name'];
            }
        }else{
            $this->_messageError['group_name'] = '';
            $this->_arrData['group_name'] = $params['group_name'];
            $this->_classError['group_name'] = '';
        }
        
        /*
        * Check Description
        */
        
        if(!$validateDescription->isValid($params['description'])){
            $this->_messageError['description'] = '<label class="control-label" for="inputError">' . $translate->_('description') . current($validateDescription->getMessages()) . '</label>';
            $this->_arrData['description'] = '';
            $this->_classError['description'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['description'] = $category['category'];
            }
        }else{
            $this->_messageError['description'] = '';
            $this->_arrData['description'] = $params['description'];
            $this->_classError['description'] = '';
        }
        
        $status = true;
        
        if(!isset($params['status'])){
            $status = false;
        }
        
        $this->_arrData['status'] = $status;
        
        if($edit){
            $this->_arrData['id'] = $category['id'];
        }
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        //$info = new Zenddn_System_Info();
        //$user_id = $info->getUserInfo('id');
        
        if(!$edit){
            $this->_arrData['created'] = $curDate;
            //$this->_arrData['created_by'] = $user_id;
        }
        
        $this->_arrData['modified'] = $curDate;
        //$this->_arrData['modified_by'] = $user_id;
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}